package org.cycuchy.hp.components.book {
	
	import com.rubenswieringa.book.Book;
	import com.rubenswieringa.book.BookEvent;
	import com.rubenswieringa.book.Page;
	
	import flash.events.Event;
	
	import org.cycuchy.hp.app.BrowserManager;
	import org.cycuchy.hp.components.book.gallery.GalleryPage;
	import org.cycuchy.hp.components.book.kapstelki.KapstelikiPage;
	import org.cycuchy.hp.event.PreloadingImageEvent;
	
	use namespace com.rubenswieringa.book.limited;
	
	public class Book_code extends com.rubenswieringa.book.Book {
		
//		private var _kapsteliki:KapstelkiData;
//		private var _fotos:FotosData;
		
//		private var _kapstelikiError:Boolean;
		
//		public var front:Page;
//		public var cyc1:Page;
		
		[Bindable]
		public var kapstelkiBrowserManager:BrowserManager;
		
		[Bindable]
		public var fotosBrowserManager:BrowserManager;
		
		public function Book_code():void {
//			trace("Book");
			super();
			kapstelkiBrowserManager = new BrowserManager();
			fotosBrowserManager = new BrowserManager();
			
//			for (var i:uint=0; i<40; i++) {
//				trace("<kapstelek beerName=\"nazwa browca\" beerCountry=\"kraj\" imgSrc=\"assets/img/kaps/" + i + ".jpg\"/>");
//			}
			
			addEventListener(BookEvent.PAGEFLIP_STARTED, flipStarted);
			addEventListener(BookEvent.PAGEFLIP_FINISHED, flipFinished);
			addEventListener(BookEvent.PAGE_TURNED, pageTurned);
			addEventListener(BookEvent.CURRENTPAGE_CHANGED, currentPageChanged);
//			addEventListener(BookEvent.PAGEFLIP_ENDING, pfe);
//			addEventListener(KapstelikiXMLParsedEvent.DONE, kapselkiXMLParsed);
		}
		
//		public function set kapstelikiError(kapstelikiError:Boolean):void {
//			this._kapstelikiError = kapstelikiError;
//		}

//		public function set kapstelikiData(kapsteliki:KapstelkiData):void {
//			this._kapsteliki = kapsteliki;
//		}

//		public function set fotosData(fotos:FotosData):void {
//			this._fotos = fotos;
//		}
		
		override protected function evaluateHover(event:Event=null):void {
//			trace("Book:evaluateHover");
			if (visible) {
				super.evaluateHover(event);				
			}
		}
		
//		override protected function createDynamicPages():void {
//			trace("Book::createDynamicPages");
//			addKapstelki();
//			addGallery();
//		}
		
//		private function addChapter(text1:String, text2:String, name:String):void {
//			this.addChild(new ChapterBreakPage());
//			var chapterPage:ChapterPage = new ChapterPage();
//			chapterPage.name = name;
//			chapterPage.text1 = text1;
//			chapterPage.text2 = text2;
//			this.addChild(chapterPage);
//		}

		private function pageTurned(event:BookEvent):void {
			trace("pt: " + event.page.index)
			dispatch(PreloadingImageEvent.LOAD, event.page.index);
		}
		
		private function flipStarted(event:BookEvent):void {
			dispatch(PreloadingImageEvent.CANT_ZOOM);
		}

		private function flipFinished(event:BookEvent):void {
//			trace("ff: " + event.page.index)
			dispatch(PreloadingImageEvent.CAN_ZOOM);
		}
		
		private function dispatch(event:String, index:int=-1):void {
			for each (var page:Page in pages) {
				if (page is KapstelikiPage || page is GalleryPage) {
					page.dispatchEvent(new PreloadingImageEvent(event, index));
				}
			}
		}
		
//		private function pfe(event:BookEvent):void {
//			trace("pfe: " + event.page.index)
//		}
		
//		private function addKapstelki():void {
////			trace("Book:addKapstelki _kapsteliki: " + _kapsteliki);
//			if (_kapsteliki == null) {
//				
//			} else {
//				var n:uint = _kapsteliki.rows * _kapsteliki.cols;
//				var len:uint = _kapsteliki.kapstelki.length;
//				var count:uint = len/n + (len%n != 0 ? 1 : 0);
//				var page:KapstelikiPage;
//				for (var p:uint=0; p<count; p++) {
//					page = new KapstelikiPage(_kapsteliki, p);
//					this.addChild(page);
//				}
//				
//				if (_pages.length%2 == 0) {
//					page = new KapstelikiPage(_kapsteliki, len);
//					this.addChild(page);
//				}
//			}
//		}
		
//		private function addGallery():void {
//			addChapter("FOTKI", "Zdjęcia", "fotki");
//			if (_fotos == null) {
//			} else {
//				var n:uint = _fotos.rows * _fotos.cols;
//				var len:uint = _fotos.fotos.length;
//				var count:uint = len/n + (len%n != 0 ? 1 : 0);
//				var page:GalleryPage;
//				for (var p:uint=0; p<count; p++) {
//					page = new GalleryPage(_fotos, p);
//					this.addChild(page);
//				}
//				
//				if (_pages.length%2 == 0) {
//					var pa:ChapterBreakPage = new ChapterBreakPage();
//					pa.addBackground = false;
//					pa.setStyle("backgroundColor", 0xFFFFFF);
//					pa.gSide = "right";					 
//					this.addChild(pa);
//				}
//			}
//		}
		
		private function currentPageChanged(event:Event):void {
//			trace("currentPageChanged");
			getChildByName("mapControlPage").dispatchEvent(event);
		}
			
		public function gotoFrontPage(event:Event):void {
			gotoPage(getChildByName("front"));
		}		 
		
		public function gotoCycuchyPage(event:Event):void {
			gotoPage(getChildByName("cycuchy"));
		}		 
		
		public function gotoMapPage(event:Event):void {
			gotoPage(getChildByName("map"));
		}		 
	
		public function gotoHobbyPage(event:Event):void {
			gotoPage(getChildByName("hobby"));
		}

		public function gotoFotkiPage(event:Event):void {
			gotoPage(getChildByName("fotki"));
		}
		
		public function gotoMiejscowkiPage(event:Event):void {
			gotoPage(getChildByName("miejscowki"));
		}
		
	}
	
}